package com.odm.communitymanage.ui.mine.MyIdle

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.LogUtils
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.odm.communitymanage.R
import com.odm.communitymanage.core.bean.MyIdleData
import com.odm.communitymanage.util.GlideUtils
import org.w3c.dom.Text

/**
 * @description: 我的闲置 列表 适配器
 * @author: ODM
 * @date: 2020/4/18
 */
class MyIdleAdapter(data : MutableList<MyIdleData>):
BaseQuickAdapter<MyIdleData , BaseViewHolder>(R.layout.item_idle_mine , data) {

    override fun convert(holder: BaseViewHolder, item: MyIdleData) {
        holder.setText(R.id.tvPrice_MyIdle, "￥" + item.price)
        holder.setText(R.id.tvCommodityTitle_MyIdle, item.topic)
        holder.setText(R.id.tvContent_MyIdle, item.description)
        when (item.status) {
            0 -> {
                //正在出售
                holder.setText(R.id.tvState_MyIdle, "正在出售")
                holder.getView<TextView>(R.id.tvState_MyIdle)
                    .setTextColor(ColorUtils.getColor(R.color.help_unsloved))
            }

            1 -> {
                //交易成功
                holder.setText(R.id.tvState_MyIdle, "交易成功")
                holder.getView<TextView>(R.id.tvState_MyIdle)
                    .setTextColor(ColorUtils.getColor(R.color.help_solved))
            }
            else -> {
            }
        }
        //没图片时 imgs 为 null
        item.imgs?.let {
            val iv0: ImageView = holder.getView<ImageView>(R.id.ivPhoto_0_MyIdle)
            val iv1: ImageView = holder.getView<ImageView>(R.id.ivPhoto_1_MyIdle)
            val iv2: ImageView = holder.getView<ImageView>(R.id.ivPhoto_2_MyIdle)
            val imageViews: Array<ImageView> = arrayOf(iv0, iv1, iv2)

            if (item.imgs.isEmpty()) {
                //无图片时全部清空遗留的viewHolder
                for (iv in imageViews) {
//                    Glide.with(context).clear(iv)
                    iv.visibility = View.GONE
                }
                return
            }
            //按顺序加载图片
            if (item.imgs.isNotEmpty()) {
                //根据图片数量限制 item的图片显示
                when(item.imgs.size){
                    1 -> {
                        imageViews[1].visibility = View.GONE
                        imageViews[2].visibility = View.GONE
                    }
                    2 -> {
                        imageViews[2].visibility = View.GONE
                    }
                }

                for (index in item.imgs.indices) {
                    val url = item.imgs[index]
                    GlideUtils.getInstance()
                        .loadImageUrl(context, url, imageViews[index])
                    imageViews[index].visibility = View.VISIBLE

/*                    val tag = imageViews[index].getTag(R.id.recyclerview_image_url)
                    if (tag != null && tag != url) {
                        Glide.with(context).clear(imageViews[index])
                        imageViews[index].visibility = View.GONE
                    } else {
                        GlideUtils.getInstance()
                            .loadImageUrl(context, url, imageViews[index])
                        imageViews[index].visibility = View.VISIBLE
                        imageViews[index].setTag(R.id.recyclerview_image_url, url)
                    }*/
                }

            }
        }

    }
}

    class MyIdleDiffCallback(private val oldList: List<MyIdleData> ?, private val newList: List<MyIdleData> ?) :
        DiffUtil.Callback() {

        override fun getOldListSize(): Int {
            return oldList?.size ?: 0
        }

        override fun getNewListSize(): Int {
            return newList?.size ?: 0
        }

        override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
            return oldList?.get(oldItemPosition)?.javaClass == newList?.get(newItemPosition)?.javaClass
        }

        override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
            return  oldList?.get(oldItemPosition)?.id == newList?.get(newItemPosition)?.id &&
                    oldList?.get(oldItemPosition)?.imgs == newList?.get(newItemPosition)?.imgs
        }
    }

